"use strict";
/* eslint-disable jsdoc/ts-no-empty-object-type */

// const { func } = require('effect/FastCheck');
const { HTMLElement, Node, parse } = require("node-html-parser");

/**
 * @param {string} content
 * @param {import('webpack').LoaderContext<{}>} loaderContext
 * @returns {string}
 */
function insertDataInspect(content, loaderContext) {
  const root = parse(
    String.raw({
      raw: content,
    }),
  );
  /**
   * @param {unknown} n
   * @returns {n is HTMLElement}
   */
  const isHTMLElement = (n) => n instanceof HTMLElement;
  /**
   * @param {Node} node
   */
  function doSetDataInspect(node) {
    if (isHTMLElement(node)) {
      node.setAttribute("data-inspect", "nhp");
      // eslint-disable-next-line unicorn/no-array-for-each
      node.childNodes.forEach(doSetDataInspect);
    }
  }
  doSetDataInspect(root);
  return root.toString();
}

module.exports = { insertDataInspect };
// console.log(root.toString());
// ul#list
//   li
//     #text

// console.log(root.querySelector('#list'));
// { tagName: 'ul',
//   rawAttrs: 'id="list"',
//   childNodes:
//    [ { tagName: 'li',
//        rawAttrs: '',
//        childNodes: [Object],
//        classNames: [] } ],
//   id: 'list',
//   classNames: [] }
// console.log(root.toString());
// <ul id="list"><li>Hello World</li></ul>
// root.set_content('<li>Hello World</li>');
// root.toString();	// <li>Hello World</li>
